package com.organize.controllers;

import com.autumn.application.dto.DefaultEntityDto;
import com.autumn.application.dto.input.DefaultAdvancedPageQueryInput;
import com.autumn.mybatis.mapper.PageResult;
import com.autumn.security.annotation.RequestCSRFProtect;
import com.autumn.zero.common.library.application.dto.input.CommonStatusInput;
import com.autumn.zero.common.library.application.dto.tree.input.DefaultChildrenQueryInput;
import com.organize.application.dto.department.DepartmentInput;
import com.organize.application.dto.department.DepartmentOutput;
import com.organize.application.services.DepartmentAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门管理
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-10-17 15:39
 **/
@RestController
@RequestMapping("/org/department")
@Api(tags = "部门管理")
@RequiresUser
// 启用 CSRF 攻击防护，需要配置文件启用才有效
@RequestCSRFProtect
public class DepartmentController {

    private final DepartmentAppService service;

    public DepartmentController(DepartmentAppService service) {
        this.service = service;
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加")
    public DepartmentOutput add(@Valid @RequestBody DepartmentInput input) {
        return service.add(input);
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public DepartmentOutput update(@Valid @RequestBody DepartmentInput input) {
        return service.update(input);
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "根据id删除")
    public void deleteById(@Valid @RequestBody DefaultEntityDto input) {
        service.deleteById(input.getId());
    }

    /**
     * 更新状态
     *
     * @param input
     * @return
     */
    @PostMapping("/update/status")
    @ApiOperation(value = "更新状态")
    public void updateStatus(@Valid @RequestBody CommonStatusInput input) {
        service.updateStatus(input);
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/query/single")
    @ApiOperation(value = "根据id获取")
    public DepartmentOutput queryById(@Valid @RequestBody DefaultEntityDto input) {
        return service.queryById(input.getId());
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/query/page")
    @ApiOperation(value = "分页查询")
    public PageResult<DepartmentOutput> queryForPage(@Valid @RequestBody DefaultAdvancedPageQueryInput input) {
        return service.queryForPage(input);
    }

    /**
     * 查询子级
     *
     * @param input
     * @return
     */
    @PostMapping("/query/children")
    @ApiOperation(value = "查询子级")
    public List<DepartmentOutput> queryChildren(@Valid @RequestBody DefaultChildrenQueryInput input) {
        return service.queryChildren(input);
    }

}
